const fs = require('fs');
const http = require('http');
const server = http.createServer((req, res) => {
    // 设置响应头
    res.setHeader('Content-Type', 'text/html;charset=utf-8')

    let {pathname} = new URL(req.url, 'http://localhost:8989');
    if (pathname==='/') {
        pathname = '/index.html'
    }
    const filePath = __dirname + '/public' + pathname;
    fs.readFile(filePath, (err, data) => {
        if (err) {
            res.end('<h1>404 Not Found</h1>')
        } else {
          res.end(data)
        }
    })
    
    

})
server.listen('8989', () => {
    console.log('启动服务成功');
})